<?php

namespace Cohuna\Controllers;

use Cohuna\Models\CohunaMember;
use Cohuna\Models\CohunaPermissionResource;
use Phalcon\Mvc\Controller;

/**
 * 控制器基类
 * @package CukeDaojia\CukeManagement\Controllers
 * @property \Phalcon\Cache\BackendInterface $cache 缓存操作
 * @property \Phalcon\Logger\Multiple $logger 日志记录
 * @property \Phalcon\Config $config 配置
 * @property \Phalcon\Db\AdapterInterface $db
 * @property \Phalcon\Mvc\Dispatcher|\Phalcon\Mvc\DispatcherInterface $dispatcher
 * @property \Phalcon\Mvc\Router|\Phalcon\Mvc\RouterInterface $router
 * @property \Phalcon\Mvc\Url|\Phalcon\Mvc\UrlInterface $url
 * @property \Phalcon\Http\Request|\Phalcon\Http\RequestInterface $request
 * @property \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface $response
 * @property \Phalcon\Http\Response\Cookies|\Phalcon\Http\Response\CookiesInterface $cookies
 * @property \Phalcon\Filter|\Phalcon\FilterInterface $filter
 * @property \Phalcon\Flash\Direct $flash
 * @property \Phalcon\Flash\Session $flashSession
 * @property \Phalcon\Session\Adapter\Files|\Phalcon\Session\Adapter|\Phalcon\Session\AdapterInterface $session
 * @property \Phalcon\Events\Manager|\Phalcon\Events\ManagerInterface $eventsManager
 * @property \Phalcon\Security $security
 * @property \Phalcon\Crypt|\Phalcon\CryptInterface $crypt
 * @property \Phalcon\Tag $tag
 * @property \Phalcon\Escaper|\Phalcon\EscaperInterface $escaper
 * @property \Phalcon\Annotations\Adapter\Memory|\Phalcon\Annotations\Adapter $annotations
 * @property \Phalcon\Mvc\Model\Manager|\Phalcon\Mvc\Model\ManagerInterface $modelsManager
 * @property \Phalcon\Mvc\Model\MetaData\Memory|\Phalcon\Mvc\Model\MetadataInterface $modelsMetadata
 * @property \Phalcon\Mvc\Model\Transaction\Manager|\Phalcon\Mvc\Model\Transaction\ManagerInterface $transactionManager
 * @property \Phalcon\Assets\Manager $assets
 * @property \Phalcon\Di|\Phalcon\DiInterface $di
 * @property \Phalcon\Session\Bag|\Phalcon\Session\BagInterface $persistent
 * @property \Phalcon\Mvc\View|\Phalcon\Mvc\ViewInterface $view
 * @property \Redis $redis;
 * @property \Phalcon\Security\Random $random
 * @property \Phalcon\Acl\Adapter\Memory $acl
 */
class ControllerBase extends Controller
{
    const PageSize = 15;

    public function initialize()
    {
        $this->view->setVar('Version',COHUNA_VERSION);
        $this->view->setVar('Controller',$this->dispatcher->getControllerName());
        $this->session->set('member_id',1);
        $member_id = $this->session->get('member_id');

        $member = CohunaMember::findFirstByMemberId($member_id);

        $menu_list = CohunaPermissionResource::find(array('is_show_menu = :is_show_menu:','bind'=>array('is_show_menu'=>1),'order'=>'sort_index ASC'));
        if($menu_list !== false){
            $resource_list = array();
            if($member->role_id == 0){
                $resource_list = $menu_list;
            }else {
                foreach ($menu_list as $menu) {
                    foreach ($member->permission_list as $resource) {
                        if($menu->resource_id == $resource->resource_id){
                            $resource_list[] = $menu;
                            break;
                        }
                    }
                }
            }
        }else{
            $resource_list = array();
        }
       // var_dump($resource_list);exit;
        $this->view->setVar('Member',$member);
        $this->view->setVar('MenuList',$resource_list);
    }
}